import fs from 'fs'
import { resolve } from 'path'
import mongoose from 'mongoose'
import config from '../config'
import R from 'ramda'

const models = resolve(__dirname, '../database/schema')

fs.readdirSync(models).filter(file => ~file.search(/^[^\.].*js$/))
.forEach(file => require(resolve(models, file)))

export const database = app => {
  mongoose.set('debug', true)
  // , {useNewUrlParser:true},
  mongoose.connect(config.db)
  mongoose.connection.on('disconnected', () => {
    mongoose.connect(config.db)
  })
  mongoose.connection.on('error', (err) => {
    console.error(err,'mongoose 链接失败')
  })
  mongoose.connection.on('open', async () => {
    console.log('mongoDB open：', config.db)
   
    
  })
}
